package com.servlet;

import com.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login02.do")
public class Login02Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //第一次调用：创建Session对象
        //后续调用：返回创建过的Session对象
        HttpSession session = request.getSession();
        //判断该Session是否为刚创建
        //当浏览器初次访问服务器时，服务器就会创建一个Session对象
        //并且把 SessionId 通过响应返回给浏览器，作为Cookie保存
        //后续每次浏览器访问，都会带上SessionId
        System.out.println(session.isNew());
        //获取Session对象的ID
        System.out.println(session.getId());
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(username, password);
        //域对象
        session.setAttribute("user", user);
        //重定向
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }
}
